//
//  ST_BTGlobalCenter.swift
//  btvideo
//
//  Created by mac on 2020/8/6.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

/**
 伴心数据中心
 */
class ST_BTGlobalDataCenter: NSObject {
    
    static let shared = ST_BTGlobalDataCenter()
    
    var current_user = ST_BTGCBaseUserModel()
    
    fileprivate var regist_start_page:String?
    
    fileprivate var lauch_data_center = false
    
    fileprivate var logger = Log()
}

//MARK: - 注册认证
extension ST_BTGlobalDataCenter{
    
    /**
     注册成功 【事件上报】
     */
    func register_success_event() {
        
        let timeStr = Date().string(withFormat: "yyyy/MM/dd")
        
        let eventIdentifier = "registration_success"
        
        let param = ["registration_date":timeStr,"registration_version":appVersion]
        
        //MobClick.event(eventIdentifier, attributes: param)
        
    }
 
    
    /**
     待确定的上报
     face_detected
     realface
     */
    
    /**
     打开通知【事件上报】
     */
    func allow_notification_event(){
        
        let eventIdentifier = "open_message"
        
        let param = self.current_user.to_j_s_o_n()
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
}


//MARK: - 认证
extension ST_BTGlobalDataCenter{
    
     /**
      点击人脸认证【事件上报】
      */
     func start_detect_face_event(){
         
         let eventIdentifier = "click_certification"
         
         let param = self.current_user.to_j_s_o_n()
         
         //MobClick.event(eventIdentifier, attributes: param)
     }
    
    
    /**
     百度检测人脸完成【事件上报】
     */
    func ai_detect_face_event(){
        
        let eventIdentifier = "face_detected"
        
        let param = self.current_user.to_j_s_o_n()
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
     
}


//MARK: - 聊天
extension ST_BTGlobalDataCenter{
    /**
     进入聊天【事件上报】
     */
    func enter_private_chat_event(sessionId:String,source:String,isFirst:Bool){
        
        let eventIdentifier = "enter_chat_page"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(sessionId, forKey: "chated_user_id")
        
        param?.updateValue(source, forKey: "source")
        
        param?.updateValue(isFirst, forKey: "is_first_chat")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
    
    /**
     发送消息【事件上报】
     */
    func send_chat_message_event(sessionId:String,isFirst:Bool,isFirstSend:Bool){
        
        let eventIdentifier = "send_message"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(sessionId, forKey: "chated_user_id")
        
        param?.updateValue(isFirstSend, forKey: "is_first_send")
        
        param?.updateValue(isFirst, forKey: "is_first_chat")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
    
    /**
     收到消息【事件上报】
     */
    func receive_chat_message_event(sessionId:String,isFirst:Bool,isFirstReceive:Bool){
        
        let eventIdentifier = "receive_message"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(sessionId, forKey: "chated_user_id")
        
        param?.updateValue(isFirstReceive, forKey: "is_first_receive")
        
        param?.updateValue(isFirst, forKey: "is_first_chat")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
    
    
    
}

//MARK: - 特质
extension ST_BTGlobalDataCenter{
    
    /**
     特质发布成功【事件上报】
     */
    func push_peculiarity_success_event(peculiarity_id:String,first_class_classification:String,second_class_classification:String){
        
        let eventIdentifier = "publish_peculiarity"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(peculiarity_id, forKey: "peculiarity_id")
        
        param?.updateValue(first_class_classification, forKey: "first_class_classification")
        
        param?.updateValue(second_class_classification, forKey: "second_class_classification")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }

    /**
     回复特质的评论【事件上报】
     */
    func reply_peculiarity_comment_event(peculiarity_id:String,reply_user_id:String,source:String,is_own_peculiarity:Bool){
        
        let eventIdentifier = "reply_peculiarity_commented"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(peculiarity_id, forKey: "peculiarity_id")
        param?.updateValue(reply_user_id, forKey: "reply_user_id")
        
        /*
        {"msg":"操作成功","code":200,"data":{"feature_name":"唱歌好听","parent_feature_name":"才华"},"time":1596802732}
        */
        JVrequest(PH.fetchPecGroupNameApi,parameters: ["group_id":peculiarity_id], success: { (json) in
            
            
            print("查询 特质名字:",json)
            
            let data = json["data"]
            
            let first_class_classification = data["parent_feature_name"].stringValue
            
            let second_class_classification = data["feature_name"].stringValue
            
            param?.updateValue(first_class_classification, forKey: "first_class_classification")
             param?.updateValue(second_class_classification, forKey: "second_class_classification")

             param?.updateValue(source, forKey: "source")
             param?.updateValue(is_own_peculiarity, forKey: "is_own_peculiarity")
             
             //MobClick.event(eventIdentifier, attributes: param)
            
        }) { (code, err) in
            
        }
    }
    
    
    
    
    

    
    
    
    /**
     评论特质【事件上报】
     */
    func commentPeculiarityEvent(peculiarity_id:String,commented_user_id:String){
        
        let eventIdentifier = "comment_peculiarity"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(peculiarity_id, forKey: "peculiarity_id")
        param?.updateValue(commented_user_id, forKey: "commented_user_id")
        
        /*
        {"msg":"操作成功","code":200,"data":{"feature_name":"唱歌好听","parent_feature_name":"才华"},"time":1596802732}
        */
        JVrequest(PH.fetchPecGroupNameApi,parameters: ["group_id":peculiarity_id], success: { (json) in
            
            
            print("查询 特质名字:",json)
            
            let data = json["data"]
            
            let first_class_classification = data["parent_feature_name"].stringValue
            
            let second_class_classification = data["feature_name"].stringValue
            
            param?.updateValue(first_class_classification, forKey: "first_class_classification")
             param?.updateValue(second_class_classification, forKey: "second_class_classification")

             //MobClick.event(eventIdentifier, attributes: param)
            
        }) { (code, err) in
            
        }
        
        
    }
    
    /**
     点赞特质
     */
    func thumbUpPeculiarityEvent(peculiarity_id:String,liked_user_id:String){
        
        let eventIdentifier = "like_peculiarity"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(peculiarity_id, forKey: "peculiarity_id")
        param?.updateValue(liked_user_id, forKey: "liked_user_id")
        
        
        JVrequest(PH.fetchPecGroupNameApi,parameters: ["group_id":peculiarity_id], success: { (json) in
            
            
            print("查询 特质名字:",json)
            
            let data = json["data"]
            
            let first_class_classification = data["parent_feature_name"].stringValue
            
            let second_class_classification = data["feature_name"].stringValue
            
            param?.updateValue(first_class_classification, forKey: "first_class_classification")
             param?.updateValue(second_class_classification, forKey: "second_class_classification")

            print("点赞特质 key:like_peculiarity   value:",param ?? "友盟空")
            
             //MobClick.event(eventIdentifier, attributes: param)
            
        }) { (code, err) in
            
        }
    }
}

//MARK: - 日常
extension ST_BTGlobalDataCenter{
    
    /**
     日常发布成功【事件上报】
     */
    func pushDailySuccessEvent(dailyType:String,daily_id:String){
        
        let eventIdentifier = "publish_daily"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(dailyType, forKey: "daily_type")
        
        param?.updateValue(daily_id, forKey: "daily_id")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
    
    /**
     回复日常的评论【事件上报】
     */
    func replyDailyCommentEvent(daily_id:String,reply_user_id:String,dailyType:String,source:String,isMyDaily:Bool){
        
        let eventIdentifier = "reply_daily_commented"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(dailyType, forKey: "daily_type")
        
        param?.updateValue(source, forKey: "source")
        
        param?.updateValue(isMyDaily, forKey: "is_own_daily")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
    
    
    /**
     评论日常【事件上报】
     */
    func commentDailyEvent(dailyType:String,daily_id:String,commented_user_id:String){
        
        let eventIdentifier = "comment_daily"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(dailyType, forKey: "daily_type")
        param?.updateValue(daily_id, forKey: "daily_id")
        param?.updateValue(commented_user_id, forKey: "commented_user_id")
        
        //MobClick.event(eventIdentifier, attributes: param)
    }
    
    /**
     点赞日常
     */
    func thumbUpDailyEvent(dailyType:String,liked_user_id:String,daily_id:String){
        
        let eventIdentifier = "like_daily"
        
        var param = self.current_user.to_j_s_o_n()
        
        param?.updateValue(dailyType, forKey: "daily_type")
        
        param?.updateValue(liked_user_id, forKey: "liked_user_id")
        
        param?.updateValue(daily_id, forKey: "daily_id")
        
        
        let array = param?.map { (item) -> String in
            return String.init(format: "%@=%@", item.key,String.init(describing: item.value))
        }
        
        let str = array?.joined(separator: ";") ?? "百度为空"
    
        print("点赞日常 统计：",str)
      
        //MobClick.event(eventIdentifier, attributes: param)
    }
}









//MARK: - 新统计
extension ST_BTGlobalDataCenter{
    
    func clickMainTab(tag:Int) {
        
        var str = ""
        switch tag {
        case 1:
            str = "特质"
        case 2:
            str = "组CP"
        case 3:
            str = "消息"
        case 4:
            str = "我的"
        default:
            str = "伴心"
        }
        
        //MobClick.event("BottomTab", attributes: ["UserTabClick":str])
        
    }
    
    
    func todayFateEvent(str:String){
        
        //MobClick.event("TodayFate", attributes: ["UserSlip":str])
    }
    
    
//    TodayFate
    
    /**
     iOS端
     事件id  FinalRegistPayPage_New_V1.0
     参数list：
     参数ID  PayPercent
     参数名称：支付页面操作比例
     参数值：
     曝光
     立即支付
     WhyPayForIt
     照片瀑布流
     Apple支付声明
     */
    
    func displayFinalRegisterViewEvent(){
        
    }
    
    
    func payImmediatelyEvent() {
        
    }
    
    
    func whyPayForItEvent()  {
        finalRegistPage(paramValue: "WhyPayForIt")
    }
    
    func clickFinalRegisterEvent()  {
        finalRegistPage(paramValue: "照片瀑布流")
    }
    
    func clickPayAgreementEvent()  {
        
    }
    
    
    
    fileprivate func finalRegistPage(paramValue:String) {
        
        let eventIdentifier = "FinalRegistPayPage_New_V1.0"
        
        //MobClick.event(eventIdentifier, attributes: ["PayPercent":paramValue])
    }
    
    
    
    
    func payMenuPage(paramValue:String) {
        
        let eventIdentifier = "PayMenuPage"
        
        //MobClick.event(eventIdentifier, attributes: ["PayChoice":paramValue])
    }
    
    func payMemberPage(paramValue:String) {
        
        let eventIdentifier = "PayMemberPage_LikeOthers"
        
        //MobClick.event(eventIdentifier, attributes: ["PayChoice":paramValue])
    }
 
    
    func signInMemberPage(paramValue:String) {
        
        let eventIdentifier = "regist_type"
        
        //MobClick.event(eventIdentifier, attributes: ["account_type":paramValue])
    }
}




struct Log: TextOutputStream {

    func write(_ string: String) {
        let fm = FileManager.default
        let log = fm.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("bitianLog.txt")
        if let handle = try? FileHandle(forWritingTo: log) {
            handle.seekToEndOfFile()
            handle.write(string.data(using: .utf8)!)
            handle.closeFile()
        } else {
            try? string.data(using: .utf8)?.write(to: log)
        }
    }
}
